import koa from 'koa';
import router from 'koa-simple-router';
import co from 'co';
import serve from 'koa-static';
import babel_co from 'babel-core/register';
import babel_po from 'babel-polyfill';
import render from 'koa-swig';
import http from 'http';
import https from 'https';
import socketIo from 'socket.io';

import initController from './controllers/initController';
import Config from './config/config';
import rootCas from 'ssl-root-cas/latest';
 
// default for all https requests 
// (whether using https directly, request, or another module) 
https.globalAgent.options.ca = rootCas.create();
const app = new koa();

app.context.render = co.wrap(render({
  root: Config.get('viewDir'),
  autoescape: true,
  cache: 'memory', // disable, set to false 
  ext: 'html',
  writeBody: false
}));
initController.init(app, router);
 
app.use(serve(Config.get('staticDir')));

const server = https.createServer(app.callback());

var io = new socketIo(server);
io.on('connection', function(){
  console.log('connect it!');
});
// app.use(async ctx => ctx.body = await ctx.render('index'));
/*
app.use(ctx => {
  ctx.body = 'Hello Koa';
});
*/
server.listen(Config.get('port'), () => {
  console.log('Listening on %d', server.address().port);
});
